package niuke.week02;

import java.util.Scanner;

/**
 * @title: MaxPathSum
 * @Author jianglei
 * @Date: 2021/11/24 17:02
 * @Version 1.0
 */
public class MaxPathSum {

    static class TreeNode {
        int val = 0;
       TreeNode left = null;
        TreeNode right = null;

        public TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }

        public TreeNode(int val) {
            this.val = val;
        }
    }

    static int res = 0;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        TreeNode[] arr = new TreeNode[n];
        for(int i = 0; i< n; i++){
            int x = sc.nextInt();
            arr[i] = new TreeNode(x);
            int y = sc.nextInt();
            if(y >= 1){
                if(arr[y-1].left == null){
                    arr[y-1].left = arr[i];
                } else {
                    arr[y-1].right = arr[i];
                }
            }
        }
        getMax(arr[0]);
        System.out.println(res);
    }


    public static int getMax (TreeNode node){
        if(node==null)
            return 0;

        int leftMax = getMax(node.left);
        int rightMax = getMax(node.right);

        res = Math.max(res,leftMax+rightMax+node.val);

        //节点存在负数
        return Math.max(0,node.val+Math.max(leftMax,rightMax));


    }


}